package com.alibaba.otter.manager.web.resource.translator;

import com.alibaba.otter.manager.biz.common.exceptions.ManagerException;
import com.alibaba.otter.manager.web.dto.RestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.naming.AuthenticationException;
import java.util.List;

/**
 * 全局异常捕获拦截器
 */
@RestControllerAdvice
public class TranslatorExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(TranslatorExceptionHandler.class);


    /**
     * 用户权限拦截
     *
     */
    @ExceptionHandler(AuthenticationException.class)
    public RestResponse<Void> authenticationException(AuthenticationException e) {
        logger.error(e.getMessage(),e);
        return RestResponse.fail(401000,e.getMessage());
    }

    /**
     * 业务异常拦截器
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public RestResponse<Void> bizExceptionHandler(IllegalArgumentException e) {
        logger.error(e.getMessage(), e);
        return RestResponse.fail(417000,e.getMessage());
    }

    /**
     * 业务异常拦截器
     *
     */
    @ExceptionHandler(IllegalStateException.class)
    public RestResponse<Void> bizExceptionHandler(IllegalStateException e) {
        logger.error(e.getMessage(), e);
        return RestResponse.fail(417000,e.getMessage());
    }


    /**
     * 业务异常拦截器
     *
     */
    @ExceptionHandler(ManagerException.class)
    public RestResponse<Void> managerExceptionHandler(ManagerException e) {
        logger.error(e.getMessage(), e);
        return RestResponse.fail(417000,e.getMessage());
    }



    /**
     * 全局异常拦截器
     */
    @ExceptionHandler(Exception.class)
    public RestResponse<Void> exceptionHandler(Exception e) {
        logger.error(e.getMessage(), e);
        return RestResponse.fail(50000,"服务错误");
    }


    @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
    public RestResponse<Void> bindExceptionHandler(Exception e) {

        StringBuilder errorBuilder = new StringBuilder();
        List<ObjectError> allErrors = getAllErrors(e);
        allErrors.forEach(error -> errorBuilder.append(error.getDefaultMessage()).append(","));
        logger.error("入参有误:{}", errorBuilder);

        if (!errorBuilder.isEmpty()) {
             return RestResponse.fail(422000,errorBuilder.substring(0, errorBuilder.length() - 1));
        }
        return RestResponse.fail(422000,"入参有误");
    }

    private List<ObjectError> getAllErrors(Exception e) {

        if (e instanceof BindException) {
            return ((BindException) e).getAllErrors();
        } else {
            return ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
        }
    }
}
